package blue.squares;

import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

/**
 * Generic dialog that can be dismissed by clicking outside the dialog.
 * 
 * @author Luisa Lu
 */
public class SimpleDialog extends Dialog{
    
    // ------------------------------------------------------------------------
    // Fields.
    // ------------------------------------------------------------------------
    
    final int _layoutID;
    final Squares _squares;
    final int _titleID;

    // ------------------------------------------------------------------------
    // Implementation.
    // ------------------------------------------------------------------------
    
    public SimpleDialog(Squares context, int titleID, int resLayoutID) {
        super(context);
        _squares = context;
        _titleID = titleID;
        _layoutID = resLayoutID;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setTitle(_titleID);
        setContentView(_layoutID);
        setCancelable(true);
        
        setupDialogButton(R.id.instruction_button, Squares.DIALOG_INSTRUCTIONS);
        setupDialogButton(R.id.setting_button, Squares.DIALOG_SETTINGS);
        
        Button restartButton = (Button) findViewById(R.id.restart_button);
        if(restartButton != null){
            final Dialog dialog = this;
            restartButton.setOnClickListener(new View.OnClickListener(){
                public void onClick(View arg0) {
                    _squares.newGame();
                    dialog.dismiss();
                }
            });
        }
        
        Button dismissButton = (Button) findViewById(R.id.dismiss_button);
        if(dismissButton != null){
            final Dialog dialog = this;
            dismissButton.setOnClickListener(new View.OnClickListener(){
                public void onClick(View arg0) {
                    dialog.dismiss();
                }
            });
        }
    }
    
    private void setupDialogButton(int buttonID, final int dialogID){
        Button button = (Button) findViewById(buttonID);
        if(button != null){
            button.setOnClickListener(new View.OnClickListener(){
                public void onClick(View arg0) {
                    _squares.showDialog(dialogID);
                }
            });
        }
    }
}
